#include <QDir>
#include <QTranslator>
#include <QIcon>
#include <QFontDatabase>

#include "SingleApplication.h"
#include "MainApp.h"
#include "glog/logging.h"

void init_glog(bool flag)
{
    if (flag)
    {
        google::InitGoogleLogging("");
        google::SetStderrLogging(google::GLOG_INFO);
        FLAGS_logtostderr = true;
        FLAGS_colorlogtostderr = false;
        FLAGS_log_prefix = true;
    }
    else
    {
        QDir dir(QApplication::applicationDirPath());
        dir.mkdir("logs");

        google::InitGoogleLogging("");
        google::SetLogDestination(google::GLOG_INFO, "logs/INFO_");
        google::SetLogDestination(google::GLOG_ERROR, "logs/ERROR_");
        google::SetLogDestination(google::GLOG_WARNING, "logs/WARNING_");
        FLAGS_log_dir = "logs";
        FLAGS_logtostderr = false;
        FLAGS_colorlogtostderr = false;				// Set log color
        FLAGS_logbufsecs = 0;						// Set log output speed(s)
        FLAGS_max_log_size = 10;					// Set max log file size
        FLAGS_stop_logging_if_full_disk = true;		// If disk is full
    }
}

int main(int argc, char *argv[])
{
	CSingleApplication a(argc, argv);

    QDir::setCurrent(QApplication::applicationDirPath());
    QApplication::setQuitOnLastWindowClosed(true);
    if (!a.Lock())
    {
        return -1;
    }

    if (argc >= 2 && std::string(argv[1]) == "-d")
    {
        init_glog(true);
    }
    else
    {
        init_glog(false);
    }
	QTranslator t1, t2;
    t1.load(":/qm/client_zh.qm");
	t2.load("./qt_zh_CN.qm");
	a.installTranslator(&t1);
	a.installTranslator(&t2);

	CMainApp mainApp;
	mainApp.Run();

	return a.exec();
}
